package com.eascs.workflow.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.wf.DicData;
import com.eascs.workflow.common.service.dictionary.DicDataService;
import com.eascs.workflow.common.service.sys.SysPermissionService;
import com.eascs.workflow.common.service.wf.WorkFlowService;
import com.eascs.workflow.common.type.SystemModule;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.UserTaskParam;
import com.eascs.workflow.common.vo.UserTaskVo;

@Controller
public class ProcessTaskController extends BaseMasterViewController {
	@Reference
	private WorkFlowService workFlowService;
	@Reference
	private SysPermissionService sysPermissionService;
	@Reference
	private DicDataService dicDataService;
	@Reference
	private ICustomerInfoService iCustomerInfoService;
	@Value("${o2o.admin.iframe.domain}")
	private String domain;
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "approvalManage");
	}
	@RequestMapping(value={"/wf/queryTask","/"})
	public String queryTask(UserTaskParam param) {
		//默认查询待办列表
		if(!StringUtil.hasText(param.getApprovalStatus())){
			param.setApprovalStatus(UserTaskParam.APPROVAL_STATUS_UNDO);
		}
		User user = UserHolder.getUser();
		PageResult<UserTaskVo> pageResult = new PageResult<>();
		param.setSystemModule(SystemModule.SYSTEM_MODULE_O2O);
		//ViewContext.put(MenuTag.CURRENT_MENU_CODE, "approvalManage");
		// 超级管理员处理
		if (param.isQueryAll()) {
			// 检查是否为超级管理员
			if (!user.isSuperAdministrator()) {
				param.setAuditorNo(user.getNo());
			}
			if(UserTaskParam.APPROVAL_STATUS_PROCESSING.equals(param.getApprovalStatus())){
				if (!user.isSuperAdministrator()) {
					param.setAppliedby(user.getNo());
				}
			}
		} else {
			param.setAuditorNo(user.getNo());
			if(UserTaskParam.APPROVAL_STATUS_PROCESSING.equals(param.getApprovalStatus())){
				param.setAppliedby(user.getNo());
			}
		}
		StringBuffer pageUrl = new StringBuffer("/wf/queryTask?1=1");
		if(StringUtil.hasText(param.getApprovalStatus())){
			pageUrl.append("&approvalStatus=").append(param.getApprovalStatus());
		}
		if(StringUtil.hasText(param.getProcessType())){
			pageUrl.append("&processType=").append(param.getProcessType());
		}
		if(StringUtil.hasText(param.getReferCode())){
			pageUrl.append("&referCode=").append(param.getReferCode());
		}
		if(StringUtil.hasText(param.getAppliedbyName())){
			pageUrl.append("&appliedbyName=").append(param.getAppliedbyName());
		}
		if(StringUtil.hasText(param.getTaskCode())){
			pageUrl.append("&taskCode=").append(param.getTaskCode());
		}
		if(StringUtil.hasText(param.getAuditorName())){
			pageUrl.append("&auditorName=").append(param.getAuditorName());
		}
		if(StringUtil.hasText(param.getLender())){
			pageUrl.append("&lender=").append(param.getLender());
		}
		if(StringUtil.hasText(param.getCatagory())){
			pageUrl.append("&catagory=").append(param.getCatagory());
		}
		if(StringUtil.hasText(param.getPnodeName())){
			pageUrl.append("&pnodeName=").append(param.getPnodeName());
		}
		boolean queryAll = param.isQueryAll();
		if(queryAll){
			pageUrl.append("&queryAll=true");
		}
		
		if(StringUtils.isNotBlank(param.getCatagory())){
			List<DicData> dicList = workFlowService.getProcessTypeByCatagory(param.getCatagory()).getModel();
			ViewContext.put("processTypeList", dicList);
			List<String> processTypeList = new ArrayList<String>();
			if(StringUtils.isNotBlank(param.getProcessType())){
				processTypeList.add(param.getProcessType());
			}else{
				for(DicData dic : dicList){
					processTypeList.add(dic.getCode());
				}
			}
			param.setProcessTypeList(processTypeList);
		}
		ModelResult<PageResult<UserTaskVo>> queryResult = workFlowService.queryUserTaskByPage(param);
		if (queryResult.isSuccess()) {
			pageResult = queryResult.getModel();
		}
		List<DicData> catagoryList = dicDataService.getDicDataByType("processCatagory").getModel();
		ViewContext.put("catagoryList", catagoryList);
		ViewContext.put("page", pageResult);
		ViewContext.put("pageUrl", pageUrl);
		ViewContext.put("userTaskParam", param);
		ViewContext.put("superAdministrator", user.isSuperAdministrator());
		if (UserTaskParam.APPROVAL_STATUS_UNDO.equals(param.getApprovalStatus())) {
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "undoTask");
		}else if (UserTaskParam.APPROVAL_STATUS_FINISH.equals(param.getApprovalStatus())) {
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "hasDoTask");
		}else if (UserTaskParam.APPROVAL_STATUS_PROCESSING.equals(param.getApprovalStatus())) {
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "doingTask");
		}
		return "/workflow/taskList";
	}
	
	@RequestMapping("/wf/queryTask/{approvalStatus}")
	public String taskDetail(@PathVariable String approvalStatus, String referId, String nodeAuditorId, HttpServletResponse response){
		try {
			response.sendRedirect("/wf/approval?loanapplyNo="+referId+"&nodeAuditorId="+nodeAuditorId+"&approvalStatus="+approvalStatus);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
